package cn.zhy.basic.controller;

import cn.zhy.basic.dto.SmsCodeDto;
import cn.zhy.basic.exception.BusinessException;
import cn.zhy.basic.service.IVerifyCodeService;
import cn.zhy.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImgCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        }catch (BusinessException e) {//加一个自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试");
        }

    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody() SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return  new AjaxResult();
        }catch (BusinessException e) {//加一个自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试");
        }

    }
    //微信绑定发送验证码
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){

        try {
            //调取生成图形验证码的方法
            verifyCodeService.binderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {//加一个自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试");
        }


    }
}
